<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>第 9 章 回归分析 | 生态统计学原理</title>
  <meta name="description" content="这是生态学统计基础的课程文件" />
  <meta name="generator" content="bookdown 0.40 and GitBook 2.6.7" />

  <meta property="og:title" content="第 9 章 回归分析 | 生态统计学原理" />
  <meta property="og:type" content="book" />
  
  <meta property="og:description" content="这是生态学统计基础的课程文件" />
  <meta name="github-repo" content="rstudio/bookdown-demo" />

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="第 9 章 回归分析 | 生态统计学原理" />
  
  <meta name="twitter:description" content="这是生态学统计基础的课程文件" />
  

<meta name="author" content="沈国春、李勤，华东师范大学生态与环境科学学院" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="经典统计检验.html"/>
<link rel="next" href="谱系分析.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />








<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>


<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<style type="text/css">
  
  div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">生态统计学原理</a></li>

<li class="divider"></li>
<li class="chapter" data-level="1" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i><b>1</b> 前言</a>
<ul>
<li class="chapter" data-level="1.1" data-path="index.html"><a href="index.html#为什么要学生态统计"><i class="fa fa-check"></i><b>1.1</b> 为什么要学生态统计</a></li>
<li class="chapter" data-level="1.2" data-path="index.html"><a href="index.html#为什么要学r语言"><i class="fa fa-check"></i><b>1.2</b> 为什么要学R语言</a></li>
<li class="chapter" data-level="1.3" data-path="index.html"><a href="index.html#如何使用和修改本书"><i class="fa fa-check"></i><b>1.3</b> 如何使用和修改本书</a></li>
<li class="chapter" data-level="1.4" data-path="index.html"><a href="index.html#课程助教"><i class="fa fa-check"></i><b>1.4</b> 课程助教</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="如何开始.html"><a href="如何开始.html"><i class="fa fa-check"></i><b>2</b> 如何开始</a>
<ul>
<li class="chapter" data-level="2.1" data-path="如何开始.html"><a href="如何开始.html#计算机的一些基本原理"><i class="fa fa-check"></i><b>2.1</b> 计算机的一些基本原理</a></li>
<li class="chapter" data-level="2.2" data-path="如何开始.html"><a href="如何开始.html#安装r程序"><i class="fa fa-check"></i><b>2.2</b> 安装R程序</a></li>
<li class="chapter" data-level="2.3" data-path="如何开始.html"><a href="如何开始.html#运行r程序"><i class="fa fa-check"></i><b>2.3</b> 运行R程序</a></li>
<li class="chapter" data-level="2.4" data-path="如何开始.html"><a href="如何开始.html#停止r程序"><i class="fa fa-check"></i><b>2.4</b> 停止R程序</a></li>
<li class="chapter" data-level="2.5" data-path="如何开始.html"><a href="如何开始.html#安装rstudio"><i class="fa fa-check"></i><b>2.5</b> 安装Rstudio</a></li>
<li class="chapter" data-level="2.6" data-path="如何开始.html"><a href="如何开始.html#获取帮助"><i class="fa fa-check"></i><b>2.6</b> 获取帮助</a>
<ul>
<li class="chapter" data-level="2.6.1" data-path="如何开始.html"><a href="如何开始.html#本地帮助文档"><i class="fa fa-check"></i><b>2.6.1</b> 本地帮助文档</a></li>
<li class="chapter" data-level="2.6.2" data-path="如何开始.html"><a href="如何开始.html#在线帮助"><i class="fa fa-check"></i><b>2.6.2</b> 在线帮助</a></li>
<li class="chapter" data-level="2.6.3" data-path="如何开始.html"><a href="如何开始.html#直接询问ai"><i class="fa fa-check"></i><b>2.6.3</b> 直接询问AI</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="如何开始.html"><a href="如何开始.html#r软件包"><i class="fa fa-check"></i><b>2.7</b> R软件包</a>
<ul>
<li class="chapter" data-level="2.7.1" data-path="如何开始.html"><a href="如何开始.html#使用r软件包"><i class="fa fa-check"></i><b>2.7.1</b> 使用R软件包</a></li>
<li class="chapter" data-level="2.7.2" data-path="如何开始.html"><a href="如何开始.html#r软件包内容"><i class="fa fa-check"></i><b>2.7.2</b> R软件包内容</a></li>
<li class="chapter" data-level="2.7.3" data-path="如何开始.html"><a href="如何开始.html#安装r软件包"><i class="fa fa-check"></i><b>2.7.3</b> 安装R软件包</a></li>
</ul></li>
<li class="chapter" data-level="2.8" data-path="如何开始.html"><a href="如何开始.html#安装其他功能"><i class="fa fa-check"></i><b>2.8</b> 安装其他功能</a>
<ul>
<li class="chapter" data-level="2.8.1" data-path="如何开始.html"><a href="如何开始.html#版本控制软件"><i class="fa fa-check"></i><b>2.8.1</b> 版本控制软件</a></li>
<li class="chapter" data-level="2.8.2" data-path="如何开始.html"><a href="如何开始.html#编写r软件包的辅助工具"><i class="fa fa-check"></i><b>2.8.2</b> 编写R软件包的辅助工具</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="如何开始.html"><a href="如何开始.html#获取本书资料"><i class="fa fa-check"></i><b>2.9</b> 获取本书资料</a>
<ul>
<li class="chapter" data-level="2.9.1" data-path="如何开始.html"><a href="如何开始.html#仅查看本书资料"><i class="fa fa-check"></i><b>2.9.1</b> 仅查看本书资料</a></li>
<li class="chapter" data-level="2.9.2" data-path="如何开始.html"><a href="如何开始.html#查看和修改本书资料"><i class="fa fa-check"></i><b>2.9.2</b> 查看和修改本书资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="r语言基础.html"><a href="r语言基础.html"><i class="fa fa-check"></i><b>3</b> R语言基础</a>
<ul>
<li class="chapter" data-level="3.1" data-path="r语言基础.html"><a href="r语言基础.html#重要的快捷键"><i class="fa fa-check"></i><b>3.1</b> 重要的快捷键</a></li>
<li class="chapter" data-level="3.2" data-path="r语言基础.html"><a href="r语言基础.html#基本数据类型"><i class="fa fa-check"></i><b>3.2</b> 基本数据类型</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="r语言基础.html"><a href="r语言基础.html#字符"><i class="fa fa-check"></i><b>3.2.1</b> 字符</a></li>
<li class="chapter" data-level="3.2.2" data-path="r语言基础.html"><a href="r语言基础.html#数字"><i class="fa fa-check"></i><b>3.2.2</b> 数字</a></li>
<li class="chapter" data-level="3.2.3" data-path="r语言基础.html"><a href="r语言基础.html#逻辑值"><i class="fa fa-check"></i><b>3.2.3</b> 逻辑值</a></li>
<li class="chapter" data-level="3.2.4" data-path="r语言基础.html"><a href="r语言基础.html#类型转换"><i class="fa fa-check"></i><b>3.2.4</b> 类型转换</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="r语言基础.html"><a href="r语言基础.html#数据运算"><i class="fa fa-check"></i><b>3.3</b> 数据运算</a>
<ul>
<li class="chapter" data-level="3.3.1" data-path="r语言基础.html"><a href="r语言基础.html#数字运算"><i class="fa fa-check"></i><b>3.3.1</b> 数字运算</a></li>
<li class="chapter" data-level="3.3.2" data-path="r语言基础.html"><a href="r语言基础.html#逻辑和关系运算"><i class="fa fa-check"></i><b>3.3.2</b> 逻辑和关系运算</a></li>
<li class="chapter" data-level="3.3.3" data-path="r语言基础.html"><a href="r语言基础.html#字符运算"><i class="fa fa-check"></i><b>3.3.3</b> 字符运算</a></li>
</ul></li>
<li class="chapter" data-level="3.4" data-path="r语言基础.html"><a href="r语言基础.html#常见运算函数"><i class="fa fa-check"></i><b>3.4</b> 常见运算函数</a></li>
<li class="chapter" data-level="3.5" data-path="r语言基础.html"><a href="r语言基础.html#变量"><i class="fa fa-check"></i><b>3.5</b> 变量</a>
<ul>
<li class="chapter" data-level="3.5.1" data-path="r语言基础.html"><a href="r语言基础.html#变量构造"><i class="fa fa-check"></i><b>3.5.1</b> 变量构造</a></li>
<li class="chapter" data-level="3.5.2" data-path="r语言基础.html"><a href="r语言基础.html#赋值"><i class="fa fa-check"></i><b>3.5.2</b> 赋值</a></li>
</ul></li>
<li class="chapter" data-level="3.6" data-path="r语言基础.html"><a href="r语言基础.html#逻辑控制"><i class="fa fa-check"></i><b>3.6</b> 逻辑控制</a>
<ul>
<li class="chapter" data-level="3.6.1" data-path="r语言基础.html"><a href="r语言基础.html#条件"><i class="fa fa-check"></i><b>3.6.1</b> 条件</a></li>
<li class="chapter" data-level="3.6.2" data-path="r语言基础.html"><a href="r语言基础.html#循环"><i class="fa fa-check"></i><b>3.6.2</b> 循环</a></li>
</ul></li>
<li class="chapter" data-level="3.7" data-path="r语言基础.html"><a href="r语言基础.html#容器"><i class="fa fa-check"></i><b>3.7</b> 容器</a>
<ul>
<li class="chapter" data-level="3.7.1" data-path="r语言基础.html"><a href="r语言基础.html#向量"><i class="fa fa-check"></i><b>3.7.1</b> 向量</a></li>
<li class="chapter" data-level="3.7.2" data-path="r语言基础.html"><a href="r语言基础.html#矩阵"><i class="fa fa-check"></i><b>3.7.2</b> 矩阵</a></li>
<li class="chapter" data-level="3.7.3" data-path="r语言基础.html"><a href="r语言基础.html#数据框"><i class="fa fa-check"></i><b>3.7.3</b> 数据框</a></li>
<li class="chapter" data-level="3.7.4" data-path="r语言基础.html"><a href="r语言基础.html#列表"><i class="fa fa-check"></i><b>3.7.4</b> 列表</a></li>
</ul></li>
<li class="chapter" data-level="3.8" data-path="r语言基础.html"><a href="r语言基础.html#函数"><i class="fa fa-check"></i><b>3.8</b> 函数</a>
<ul>
<li class="chapter" data-level="3.8.1" data-path="r语言基础.html"><a href="r语言基础.html#函数的判别"><i class="fa fa-check"></i><b>3.8.1</b> 函数的判别</a></li>
<li class="chapter" data-level="3.8.2" data-path="r语言基础.html"><a href="r语言基础.html#函数的定义"><i class="fa fa-check"></i><b>3.8.2</b> 函数的定义</a></li>
</ul></li>
<li class="chapter" data-level="3.9" data-path="r语言基础.html"><a href="r语言基础.html#作业"><i class="fa fa-check"></i><b>3.9</b> 作业</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="常见生态数据及问题.html"><a href="常见生态数据及问题.html"><i class="fa fa-check"></i><b>4</b> 常见生态数据及问题</a></li>
<li class="chapter" data-level="5" data-path="数理统计基础.html"><a href="数理统计基础.html"><i class="fa fa-check"></i><b>5</b> 数理统计基础</a>
<ul>
<li class="chapter" data-level="5.0.1" data-path="数理统计基础.html"><a href="数理统计基础.html#协方差"><i class="fa fa-check"></i><b>5.0.1</b> 协方差</a></li>
<li class="chapter" data-level="5.0.2" data-path="数理统计基础.html"><a href="数理统计基础.html#偏相关系数"><i class="fa fa-check"></i><b>5.0.2</b> 偏相关系数</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="数据探索性分析.html"><a href="数据探索性分析.html"><i class="fa fa-check"></i><b>6</b> 数据探索性分析</a>
<ul>
<li class="chapter" data-level="6.1" data-path="数据探索性分析.html"><a href="数据探索性分析.html#初步探索分析"><i class="fa fa-check"></i><b>6.1</b> 初步探索分析</a>
<ul>
<li class="chapter" data-level="6.1.1" data-path="数据探索性分析.html"><a href="数据探索性分析.html#异常值和缺失值"><i class="fa fa-check"></i><b>6.1.1</b> 异常值和缺失值</a></li>
<li class="chapter" data-level="6.1.2" data-path="数据探索性分析.html"><a href="数据探索性分析.html#理解数据"><i class="fa fa-check"></i><b>6.1.2</b> 理解数据</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="数据探索性分析.html"><a href="数据探索性分析.html#排序"><i class="fa fa-check"></i><b>6.2</b> 排序</a>
<ul>
<li class="chapter" data-level="6.2.1" data-path="数据探索性分析.html"><a href="数据探索性分析.html#排序的原理"><i class="fa fa-check"></i><b>6.2.1</b> 排序的原理</a></li>
<li class="chapter" data-level="6.2.2" data-path="数据探索性分析.html"><a href="数据探索性分析.html#排序基本原理"><i class="fa fa-check"></i><b>6.2.2</b> 排序基本原理</a></li>
<li class="chapter" data-level="6.2.3" data-path="数据探索性分析.html"><a href="数据探索性分析.html#主成分分析"><i class="fa fa-check"></i><b>6.2.3</b> 主成分分析</a></li>
<li class="chapter" data-level="6.2.4" data-path="数据探索性分析.html"><a href="数据探索性分析.html#对应分析"><i class="fa fa-check"></i><b>6.2.4</b> 对应分析</a></li>
<li class="chapter" data-level="6.2.5" data-path="数据探索性分析.html"><a href="数据探索性分析.html#常用的几种排序分析"><i class="fa fa-check"></i><b>6.2.5</b> 常用的几种排序分析</a></li>
</ul></li>
<li class="chapter" data-level="6.3" data-path="数据探索性分析.html"><a href="数据探索性分析.html#聚类"><i class="fa fa-check"></i><b>6.3</b> 聚类</a>
<ul>
<li class="chapter" data-level="6.3.1" data-path="数据探索性分析.html"><a href="数据探索性分析.html#层次聚类"><i class="fa fa-check"></i><b>6.3.1</b> 层次聚类</a></li>
<li class="chapter" data-level="6.3.2" data-path="数据探索性分析.html"><a href="数据探索性分析.html#非层次聚类"><i class="fa fa-check"></i><b>6.3.2</b> 非层次聚类</a></li>
<li class="chapter" data-level="6.3.3" data-path="数据探索性分析.html"><a href="数据探索性分析.html#多元回归树"><i class="fa fa-check"></i><b>6.3.3</b> 多元回归树</a></li>
<li class="chapter" data-level="6.3.4" data-path="数据探索性分析.html"><a href="数据探索性分析.html#顺序聚类"><i class="fa fa-check"></i><b>6.3.4</b> 顺序聚类</a></li>
</ul></li>
<li class="chapter" data-level="6.4" data-path="数据探索性分析.html"><a href="数据探索性分析.html#作业-1"><i class="fa fa-check"></i><b>6.4</b> 作业</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="实验设计.html"><a href="实验设计.html"><i class="fa fa-check"></i><b>7</b> 实验设计</a></li>
<li class="chapter" data-level="8" data-path="经典统计检验.html"><a href="经典统计检验.html"><i class="fa fa-check"></i><b>8</b> 经典统计检验</a></li>
<li class="chapter" data-level="9" data-path="回归分析.html"><a href="回归分析.html"><i class="fa fa-check"></i><b>9</b> 回归分析</a>
<ul>
<li class="chapter" data-level="9.1" data-path="回归分析.html"><a href="回归分析.html#一元回归"><i class="fa fa-check"></i><b>9.1</b> 一元回归</a>
<ul>
<li class="chapter" data-level="9.1.1" data-path="回归分析.html"><a href="回归分析.html#一元线性模型"><i class="fa fa-check"></i><b>9.1.1</b> 一元线性模型</a></li>
<li class="chapter" data-level="9.1.2" data-path="回归分析.html"><a href="回归分析.html#类型变量"><i class="fa fa-check"></i><b>9.1.2</b> 类型变量</a></li>
<li class="chapter" data-level="9.1.3" data-path="回归分析.html"><a href="回归分析.html#多项式回归"><i class="fa fa-check"></i><b>9.1.3</b> 多项式回归</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="回归分析.html"><a href="回归分析.html#多元回归"><i class="fa fa-check"></i><b>9.2</b> 多元回归</a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="回归分析.html"><a href="回归分析.html#变量筛选"><i class="fa fa-check"></i><b>9.2.1</b> 变量筛选</a></li>
<li class="chapter" data-level="9.2.2" data-path="回归分析.html"><a href="回归分析.html#因子之间的相对重要性"><i class="fa fa-check"></i><b>9.2.2</b> 因子之间的相对重要性</a></li>
<li class="chapter" data-level="9.2.3" data-path="回归分析.html"><a href="回归分析.html#模型诊断"><i class="fa fa-check"></i><b>9.2.3</b> 模型诊断</a></li>
<li class="chapter" data-level="9.2.4" data-path="回归分析.html"><a href="回归分析.html#包含类型变量的多元回归"><i class="fa fa-check"></i><b>9.2.4</b> 包含类型变量的多元回归</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="回归分析.html"><a href="回归分析.html#广义线性回归"><i class="fa fa-check"></i><b>9.3</b> 广义线性回归</a>
<ul>
<li class="chapter" data-level="9.3.1" data-path="回归分析.html"><a href="回归分析.html#目前的困境"><i class="fa fa-check"></i><b>9.3.1</b> 目前的困境</a></li>
<li class="chapter" data-level="9.3.2" data-path="回归分析.html"><a href="回归分析.html#理论扩展"><i class="fa fa-check"></i><b>9.3.2</b> 理论扩展</a></li>
<li class="chapter" data-level="9.3.3" data-path="回归分析.html"><a href="回归分析.html#binary数据例子"><i class="fa fa-check"></i><b>9.3.3</b> binary数据例子</a></li>
<li class="chapter" data-level="9.3.4" data-path="回归分析.html"><a href="回归分析.html#展示回归结果"><i class="fa fa-check"></i><b>9.3.4</b> 展示回归结果</a></li>
</ul></li>
<li class="chapter" data-level="9.4" data-path="回归分析.html"><a href="回归分析.html#混合效应模型"><i class="fa fa-check"></i><b>9.4</b> 混合效应模型</a></li>
<li class="chapter" data-level="9.5" data-path="回归分析.html"><a href="回归分析.html#结构方程"><i class="fa fa-check"></i><b>9.5</b> 结构方程</a></li>
<li class="chapter" data-level="9.6" data-path="回归分析.html"><a href="回归分析.html#作业-2"><i class="fa fa-check"></i><b>9.6</b> 作业</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="谱系分析.html"><a href="谱系分析.html"><i class="fa fa-check"></i><b>10</b> 谱系分析</a></li>
<li class="chapter" data-level="11" data-path="时间序列分析.html"><a href="时间序列分析.html"><i class="fa fa-check"></i><b>11</b> 时间序列分析</a></li>
<li class="chapter" data-level="12" data-path="空间分析.html"><a href="空间分析.html"><i class="fa fa-check"></i><b>12</b> 空间分析</a></li>
<li class="chapter" data-level="13" data-path="网络分析.html"><a href="网络分析.html"><i class="fa fa-check"></i><b>13</b> 网络分析</a>
<ul>
<li class="chapter" data-level="13.1" data-path="网络分析.html"><a href="网络分析.html#物种共现网络"><i class="fa fa-check"></i><b>13.1</b> 物种共现网络</a></li>
</ul></li>
<li class="chapter" data-level="14" data-path="宏分析.html"><a href="宏分析.html"><i class="fa fa-check"></i><b>14</b> 宏分析</a></li>
<li class="divider"></li>
<li><a href="https://faculty.ecnu.edu.cn/_s31/sgc/main.psp" target="blank">个人主页</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">生态统计学原理</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="回归分析" class="section level1 hasAnchor" number="9">
<h1><span class="header-section-number">第 9 章</span> 回归分析<a href="回归分析.html#回归分析" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<div id="一元回归" class="section level2 hasAnchor" number="9.1">
<h2><span class="header-section-number">9.1</span> 一元回归<a href="回归分析.html#一元回归" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="一元线性模型" class="section level3 hasAnchor" number="9.1.1">
<h3><span class="header-section-number">9.1.1</span> 一元线性模型<a href="回归分析.html#一元线性模型" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>自己构建一套回归的方法</p>
<div class="sourceCode" id="cb310"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb310-1"><a href="回归分析.html#cb310-1" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>)</span>
<span id="cb310-2"><a href="回归分析.html#cb310-2" tabindex="-1"></a>x<span class="ot">=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">100</span></span>
<span id="cb310-3"><a href="回归分析.html#cb310-3" tabindex="-1"></a>y<span class="ot">=</span>x<span class="sc">+</span><span class="fu">rnorm</span>(<span class="dv">100</span>,<span class="at">sd=</span><span class="dv">5</span>)</span>
<span id="cb310-4"><a href="回归分析.html#cb310-4" tabindex="-1"></a></span>
<span id="cb310-5"><a href="回归分析.html#cb310-5" tabindex="-1"></a>target_fun<span class="ot">=</span><span class="cf">function</span>(<span class="at">a0=</span><span class="dv">1</span>,<span class="at">b0=</span><span class="dv">1</span>){</span>
<span id="cb310-6"><a href="回归分析.html#cb310-6" tabindex="-1"></a>y_est<span class="ot">=</span>a0<span class="sc">+</span>b0<span class="sc">*</span>x</span>
<span id="cb310-7"><a href="回归分析.html#cb310-7" tabindex="-1"></a>target<span class="ot">=</span><span class="fu">sum</span>(<span class="fu">abs</span>(y<span class="sc">-</span>y_est))</span>
<span id="cb310-8"><a href="回归分析.html#cb310-8" tabindex="-1"></a><span class="fu">return</span>(target)  </span>
<span id="cb310-9"><a href="回归分析.html#cb310-9" tabindex="-1"></a>}</span>
<span id="cb310-10"><a href="回归分析.html#cb310-10" tabindex="-1"></a></span>
<span id="cb310-11"><a href="回归分析.html#cb310-11" tabindex="-1"></a><span class="co">#暴力破解法</span></span>
<span id="cb310-12"><a href="回归分析.html#cb310-12" tabindex="-1"></a>re<span class="ot">=</span><span class="fu">c</span>()</span>
<span id="cb310-13"><a href="回归分析.html#cb310-13" tabindex="-1"></a>as<span class="ot">=</span><span class="fu">c</span>()</span>
<span id="cb310-14"><a href="回归分析.html#cb310-14" tabindex="-1"></a>bs<span class="ot">=</span><span class="fu">c</span>()</span>
<span id="cb310-15"><a href="回归分析.html#cb310-15" tabindex="-1"></a>i<span class="ot">=</span><span class="dv">1</span></span>
<span id="cb310-16"><a href="回归分析.html#cb310-16" tabindex="-1"></a><span class="cf">for</span>(ai <span class="cf">in</span> <span class="dv">0</span><span class="sc">:</span><span class="dv">10</span>){</span>
<span id="cb310-17"><a href="回归分析.html#cb310-17" tabindex="-1"></a>  <span class="cf">for</span>(bi <span class="cf">in</span> <span class="dv">0</span><span class="sc">:</span><span class="dv">10</span>){</span>
<span id="cb310-18"><a href="回归分析.html#cb310-18" tabindex="-1"></a>    re[i]<span class="ot">=</span><span class="fu">target_fun</span>(ai,bi)</span>
<span id="cb310-19"><a href="回归分析.html#cb310-19" tabindex="-1"></a>    as<span class="ot">=</span><span class="fu">c</span>(as,ai)</span>
<span id="cb310-20"><a href="回归分析.html#cb310-20" tabindex="-1"></a>    bs<span class="ot">=</span><span class="fu">c</span>(bs,bi)</span>
<span id="cb310-21"><a href="回归分析.html#cb310-21" tabindex="-1"></a>    i<span class="ot">=</span>i<span class="sc">+</span><span class="dv">1</span></span>
<span id="cb310-22"><a href="回归分析.html#cb310-22" tabindex="-1"></a>  }</span>
<span id="cb310-23"><a href="回归分析.html#cb310-23" tabindex="-1"></a>}</span>
<span id="cb310-24"><a href="回归分析.html#cb310-24" tabindex="-1"></a>mini<span class="ot">=</span><span class="fu">which</span>(re<span class="sc">==</span><span class="fu">min</span>(re))</span>
<span id="cb310-25"><a href="回归分析.html#cb310-25" tabindex="-1"></a>a_best<span class="ot">=</span>as[mini]</span>
<span id="cb310-26"><a href="回归分析.html#cb310-26" tabindex="-1"></a>b_best<span class="ot">=</span>bs[mini]</span>
<span id="cb310-27"><a href="回归分析.html#cb310-27" tabindex="-1"></a></span>
<span id="cb310-28"><a href="回归分析.html#cb310-28" tabindex="-1"></a><span class="fu">plot</span>(<span class="at">x=</span>x,<span class="at">y=</span>y)</span>
<span id="cb310-29"><a href="回归分析.html#cb310-29" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">a=</span>a_best, <span class="at">b=</span>b_best)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-106-1.png" width="672" /></p>
<div class="sourceCode" id="cb311"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb311-1"><a href="回归分析.html#cb311-1" tabindex="-1"></a>x<span class="ot">=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">100</span></span>
<span id="cb311-2"><a href="回归分析.html#cb311-2" tabindex="-1"></a>y<span class="ot">=</span><span class="fl">1.2345</span><span class="sc">*</span>x<span class="sc">+</span><span class="fu">rnorm</span>(<span class="dv">100</span>,<span class="at">mean=</span><span class="fl">3.1415</span>,<span class="at">sd=</span><span class="dv">1</span>)</span>
<span id="cb311-3"><a href="回归分析.html#cb311-3" tabindex="-1"></a></span>
<span id="cb311-4"><a href="回归分析.html#cb311-4" tabindex="-1"></a><span class="co">#反向梯度求解</span></span>
<span id="cb311-5"><a href="回归分析.html#cb311-5" tabindex="-1"></a>target_fun2<span class="ot">=</span><span class="cf">function</span>(par){</span>
<span id="cb311-6"><a href="回归分析.html#cb311-6" tabindex="-1"></a>  <span class="fu">target_fun</span>(par[<span class="dv">1</span>],par[<span class="dv">2</span>])</span>
<span id="cb311-7"><a href="回归分析.html#cb311-7" tabindex="-1"></a>}</span>
<span id="cb311-8"><a href="回归分析.html#cb311-8" tabindex="-1"></a><span class="co">#这里默认是采用牛顿梯度法</span></span>
<span id="cb311-9"><a href="回归分析.html#cb311-9" tabindex="-1"></a>re2<span class="ot">=</span><span class="fu">optim</span>(<span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">1</span>),target_fun2)</span>
<span id="cb311-10"><a href="回归分析.html#cb311-10" tabindex="-1"></a></span>
<span id="cb311-11"><a href="回归分析.html#cb311-11" tabindex="-1"></a><span class="fu">plot</span>(<span class="at">x=</span>x,<span class="at">y=</span>y)</span>
<span id="cb311-12"><a href="回归分析.html#cb311-12" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">a=</span>re2<span class="sc">$</span>par[<span class="dv">1</span>], <span class="at">b=</span>re2<span class="sc">$</span>par[<span class="dv">2</span>])</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-107-1.png" width="672" /></p>
<p>内在的统计框架</p>
<p><span class="math display">\[y=a+bx+\epsilon\]</span></p>
<ol style="list-style-type: decimal">
<li>x测量没有任何误差</li>
<li><span class="math inline">\(\epsilon\)</span>是一个符合正态分布随机变量，均值为0，标准差为<span class="math inline">\(\delta\)</span>。</li>
<li>y一个符合正态分布的随机变量，其中均值为<span class="math inline">\(a+bx\)</span>，标准差为<span class="math inline">\(\delta\)</span></li>
</ol>
<p>R中还内置了一个非常方便的函数来求解回归系数。</p>
<div class="sourceCode" id="cb312"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb312-1"><a href="回归分析.html#cb312-1" tabindex="-1"></a>md1<span class="ot">=</span><span class="fu">lm</span>(y<span class="sc">~</span>x)</span>
<span id="cb312-2"><a href="回归分析.html#cb312-2" tabindex="-1"></a>md1</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##       3.063        1.235</code></pre>
<div class="sourceCode" id="cb314"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb314-1"><a href="回归分析.html#cb314-1" tabindex="-1"></a><span class="fu">plot</span>(<span class="at">x=</span>x,<span class="at">y=</span>y)</span>
<span id="cb314-2"><a href="回归分析.html#cb314-2" tabindex="-1"></a><span class="fu">abline</span>(md1)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-109-1.png" width="672" /></p>
<div class="sourceCode" id="cb315"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb315-1"><a href="回归分析.html#cb315-1" tabindex="-1"></a><span class="fu">plot</span>(<span class="at">x=</span>x,<span class="at">y=</span>y)</span>
<span id="cb315-2"><a href="回归分析.html#cb315-2" tabindex="-1"></a><span class="fu">abline</span>(md1)</span>
<span id="cb315-3"><a href="回归分析.html#cb315-3" tabindex="-1"></a>y_pred<span class="ot">=</span><span class="fu">predict</span>(md1)</span>
<span id="cb315-4"><a href="回归分析.html#cb315-4" tabindex="-1"></a><span class="fu">points</span>(<span class="at">x=</span>x,<span class="at">y=</span>y_pred,<span class="at">col=</span><span class="dv">2</span>,<span class="at">pch=</span><span class="dv">19</span>)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-110-1.png" width="672" /></p>
<div class="sourceCode" id="cb316"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb316-1"><a href="回归分析.html#cb316-1" tabindex="-1"></a>delta<span class="ot">=</span><span class="fu">sd</span>(y<span class="sc">-</span>y_pred)</span>
<span id="cb316-2"><a href="回归分析.html#cb316-2" tabindex="-1"></a>delta</span></code></pre></div>
<pre><code>## [1] 0.9575879</code></pre>
<p>参数的置信区间</p>
<div class="sourceCode" id="cb318"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb318-1"><a href="回归分析.html#cb318-1" tabindex="-1"></a><span class="fu">confint</span>(md1)</span></code></pre></div>
<pre><code>##                2.5 %   97.5 %
## (Intercept) 2.677706 3.447461
## x           1.228697 1.241931</code></pre>
<p>参数与零是否有显著差异</p>
<div class="sourceCode" id="cb320"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb320-1"><a href="回归分析.html#cb320-1" tabindex="-1"></a><span class="fu">summary</span>(md1)</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.8688 -0.5905 -0.1312  0.5375  2.3291 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(&gt;|t|)    
## (Intercept) 3.062584   0.193945   15.79   &lt;2e-16 ***
## x           1.235314   0.003334  370.50   &lt;2e-16 ***
## ---
## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
## 
## Residual standard error: 0.9625 on 98 degrees of freedom
## Multiple R-squared:  0.9993, Adjusted R-squared:  0.9993 
## F-statistic: 1.373e+05 on 1 and 98 DF,  p-value: &lt; 2.2e-16</code></pre>
<div id="模型的选择" class="section level4 hasAnchor" number="9.1.1.1">
<h4><span class="header-section-number">9.1.1.1</span> 模型的选择<a href="回归分析.html#模型的选择" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>针对最简单的回归模型，我们可以有三个备选模型</p>
<p><span class="math display">\[y=a+bx+\epsilon\]</span>
<span class="math display">\[y=bx+\epsilon\]</span>
<span class="math display">\[y=a+\epsilon\]</span>
到底选哪个模型？</p>
<div class="sourceCode" id="cb322"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb322-1"><a href="回归分析.html#cb322-1" tabindex="-1"></a>y<span class="ot">=</span>x<span class="sc">+</span><span class="fu">rnorm</span>(<span class="dv">100</span>,<span class="at">sd=</span><span class="dv">10</span>)</span>
<span id="cb322-2"><a href="回归分析.html#cb322-2" tabindex="-1"></a>md1<span class="ot">=</span><span class="fu">lm</span>(y<span class="sc">~</span>x)</span>
<span id="cb322-3"><a href="回归分析.html#cb322-3" tabindex="-1"></a>md1</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##    -0.08886      1.00764</code></pre>
<div class="sourceCode" id="cb324"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb324-1"><a href="回归分析.html#cb324-1" tabindex="-1"></a><span class="fu">summary</span>(md1)</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -29.0447  -4.9129  -0.0858   6.7418  26.0155 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(&gt;|t|)    
## (Intercept) -0.08886    2.09427  -0.042    0.966    
## x            1.00764    0.03600  27.987   &lt;2e-16 ***
## ---
## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
## 
## Residual standard error: 10.39 on 98 degrees of freedom
## Multiple R-squared:  0.8888, Adjusted R-squared:  0.8877 
## F-statistic: 783.3 on 1 and 98 DF,  p-value: &lt; 2.2e-16</code></pre>
<div class="sourceCode" id="cb326"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb326-1"><a href="回归分析.html#cb326-1" tabindex="-1"></a>md2<span class="ot">=</span><span class="fu">lm</span>(y<span class="sc">~</span>x<span class="dv">-1</span>)</span>
<span id="cb326-2"><a href="回归分析.html#cb326-2" tabindex="-1"></a>md2</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = y ~ x - 1)
## 
## Coefficients:
##     x  
## 1.006</code></pre>
<div class="sourceCode" id="cb328"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb328-1"><a href="回归分析.html#cb328-1" tabindex="-1"></a>md3<span class="ot">=</span><span class="fu">lm</span>(y<span class="sc">~</span><span class="dv">1</span>)</span>
<span id="cb328-2"><a href="回归分析.html#cb328-2" tabindex="-1"></a>md3</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = y ~ 1)
## 
## Coefficients:
## (Intercept)  
##        50.8</code></pre>
<div class="sourceCode" id="cb330"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb330-1"><a href="回归分析.html#cb330-1" tabindex="-1"></a><span class="fu">plot</span>(<span class="at">x=</span>x,<span class="at">y=</span>y)</span>
<span id="cb330-2"><a href="回归分析.html#cb330-2" tabindex="-1"></a><span class="fu">abline</span>(md1)</span>
<span id="cb330-3"><a href="回归分析.html#cb330-3" tabindex="-1"></a><span class="fu">abline</span>(md2,<span class="at">col=</span><span class="dv">2</span>)</span>
<span id="cb330-4"><a href="回归分析.html#cb330-4" tabindex="-1"></a><span class="fu">abline</span>(md3,<span class="at">col=</span><span class="dv">3</span>)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-118-1.png" width="672" /></p>
</div>
<div id="模型评价" class="section level4 hasAnchor" number="9.1.1.2">
<h4><span class="header-section-number">9.1.1.2</span> 模型评价<a href="回归分析.html#模型评价" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>解释度</p>
<div class="sourceCode" id="cb331"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb331-1"><a href="回归分析.html#cb331-1" tabindex="-1"></a>x<span class="ot">=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">100</span></span>
<span id="cb331-2"><a href="回归分析.html#cb331-2" tabindex="-1"></a>y<span class="ot">=</span><span class="fl">5.5</span><span class="sc">*</span>x<span class="sc">+</span><span class="fu">rnorm</span>(<span class="dv">100</span>,<span class="at">sd=</span><span class="dv">20</span>,<span class="at">mean=</span><span class="dv">10</span>)</span>
<span id="cb331-3"><a href="回归分析.html#cb331-3" tabindex="-1"></a></span>
<span id="cb331-4"><a href="回归分析.html#cb331-4" tabindex="-1"></a>md1<span class="ot">=</span><span class="fu">lm</span>(y<span class="sc">~</span>x)</span>
<span id="cb331-5"><a href="回归分析.html#cb331-5" tabindex="-1"></a>md2<span class="ot">=</span><span class="fu">lm</span>(y<span class="sc">~</span><span class="dv">1</span>)</span>
<span id="cb331-6"><a href="回归分析.html#cb331-6" tabindex="-1"></a><span class="fu">plot</span>(<span class="at">x=</span>x,<span class="at">y=</span>y)</span>
<span id="cb331-7"><a href="回归分析.html#cb331-7" tabindex="-1"></a><span class="fu">abline</span>(md1)</span>
<span id="cb331-8"><a href="回归分析.html#cb331-8" tabindex="-1"></a><span class="fu">abline</span>(md2,<span class="at">col=</span><span class="dv">2</span>)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-119-1.png" width="672" /></p>
<p>残差和</p>
<div class="sourceCode" id="cb332"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb332-1"><a href="回归分析.html#cb332-1" tabindex="-1"></a><span class="fu">var</span>(<span class="fu">predict</span>(md1)<span class="sc">-</span>y)</span></code></pre></div>
<pre><code>## [1] 390.986</code></pre>
<div class="sourceCode" id="cb334"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb334-1"><a href="回归分析.html#cb334-1" tabindex="-1"></a><span class="fu">var</span>(<span class="fu">predict</span>(md2)<span class="sc">-</span>y)</span></code></pre></div>
<pre><code>## [1] 25361.28</code></pre>
<div class="sourceCode" id="cb336"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb336-1"><a href="回归分析.html#cb336-1" tabindex="-1"></a>sm1<span class="ot">=</span><span class="fu">summary</span>(md1)</span>
<span id="cb336-2"><a href="回归分析.html#cb336-2" tabindex="-1"></a>sm2<span class="ot">=</span><span class="fu">summary</span>(md2)</span></code></pre></div>
<p>诊断模型</p>
<ol style="list-style-type: decimal">
<li>残差是否符合正态</li>
</ol>
<div class="sourceCode" id="cb337"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb337-1"><a href="回归分析.html#cb337-1" tabindex="-1"></a><span class="fu">shapiro.test</span>(<span class="fu">resid</span>(md1))</span></code></pre></div>
<pre><code>## 
##  Shapiro-Wilk normality test
## 
## data:  resid(md1)
## W = 0.98428, p-value = 0.2816</code></pre>
<ol start="2" style="list-style-type: decimal">
<li>方差是否齐性</li>
</ol>
<div class="sourceCode" id="cb339"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb339-1"><a href="回归分析.html#cb339-1" tabindex="-1"></a>y2<span class="ot">=</span>x<span class="sc">*</span><span class="fl">1.3</span><span class="sc">+</span><span class="fu">rnorm</span>(<span class="dv">100</span>,<span class="at">sd=</span><span class="dv">2</span><span class="sc">*</span>x)</span>
<span id="cb339-2"><a href="回归分析.html#cb339-2" tabindex="-1"></a><span class="co">#par(mfrow=c(2,1))</span></span>
<span id="cb339-3"><a href="回归分析.html#cb339-3" tabindex="-1"></a><span class="fu">plot</span>(<span class="at">x=</span>x,<span class="at">y=</span>y)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-123-1.png" width="672" /></p>
<div class="sourceCode" id="cb340"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb340-1"><a href="回归分析.html#cb340-1" tabindex="-1"></a><span class="fu">plot</span>(<span class="at">x=</span>x,<span class="at">y=</span>y2)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-123-2.png" width="672" /></p>
<div class="sourceCode" id="cb341"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb341-1"><a href="回归分析.html#cb341-1" tabindex="-1"></a><span class="fu">plot</span>(md1)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-124-1.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-124-2.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-124-3.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-124-4.png" width="672" /></p>
<div class="sourceCode" id="cb342"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb342-1"><a href="回归分析.html#cb342-1" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">lm</span>(y2<span class="sc">~</span>x))</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-125-1.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-125-2.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-125-3.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-125-4.png" width="672" /></p>
<div class="sourceCode" id="cb343"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb343-1"><a href="回归分析.html#cb343-1" tabindex="-1"></a>y3<span class="ot">=</span>y</span>
<span id="cb343-2"><a href="回归分析.html#cb343-2" tabindex="-1"></a>y3[<span class="dv">3</span>]<span class="ot">=</span><span class="dv">1000</span></span>
<span id="cb343-3"><a href="回归分析.html#cb343-3" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">lm</span>(y3<span class="sc">~</span>x))</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-126-1.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-126-2.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-126-3.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-126-4.png" width="672" /></p>
</div>
</div>
<div id="类型变量" class="section level3 hasAnchor" number="9.1.2">
<h3><span class="header-section-number">9.1.2</span> 类型变量<a href="回归分析.html#类型变量" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb344"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb344-1"><a href="回归分析.html#cb344-1" tabindex="-1"></a><span class="fu">str</span>(da)</span></code></pre></div>
<pre><code>## &#39;data.frame&#39;:    100 obs. of  2 variables:
##  $ x: logi  TRUE TRUE TRUE TRUE TRUE TRUE ...
##  $ y: num  120 119 115 120 124 ...</code></pre>
<div class="sourceCode" id="cb346"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb346-1"><a href="回归分析.html#cb346-1" tabindex="-1"></a>md1<span class="ot">=</span><span class="fu">lm</span>(y<span class="sc">~</span>x,<span class="at">data=</span>da)</span>
<span id="cb346-2"><a href="回归分析.html#cb346-2" tabindex="-1"></a>md1</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = y ~ x, data = da)
## 
## Coefficients:
## (Intercept)        xTRUE  
##       319.9       -200.1</code></pre>
<div class="sourceCode" id="cb348"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb348-1"><a href="回归分析.html#cb348-1" tabindex="-1"></a><span class="fu">plot</span>(<span class="at">x=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">100</span>,<span class="at">y=</span>y,<span class="at">col=</span>x<span class="sc">+</span><span class="dv">1</span>)</span>
<span id="cb348-2"><a href="回归分析.html#cb348-2" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h=</span><span class="fu">coef</span>(md1)[<span class="dv">1</span>],<span class="at">col=</span><span class="dv">1</span>)</span>
<span id="cb348-3"><a href="回归分析.html#cb348-3" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h=</span><span class="fu">sum</span>(<span class="fu">coef</span>(md1)),<span class="at">col=</span><span class="dv">2</span>)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-130-1.png" width="672" /></p>
</div>
<div id="多项式回归" class="section level3 hasAnchor" number="9.1.3">
<h3><span class="header-section-number">9.1.3</span> 多项式回归<a href="回归分析.html#多项式回归" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>针对下面的数据，用什么模型比较好呢？</p>
<p><img src="_main_files/figure-html/unnamed-chunk-131-1.png" width="672" /></p>
<p>简单直线回归</p>
<div class="sourceCode" id="cb349"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb349-1"><a href="回归分析.html#cb349-1" tabindex="-1"></a>md1<span class="ot">=</span><span class="fu">lm</span>(y<span class="sc">~</span>x)</span>
<span id="cb349-2"><a href="回归分析.html#cb349-2" tabindex="-1"></a><span class="fu">plot</span>(<span class="at">x=</span>x,<span class="at">y=</span>y);<span class="fu">abline</span>(md1)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-132-1.png" width="672" /></p>
<div class="sourceCode" id="cb350"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb350-1"><a href="回归分析.html#cb350-1" tabindex="-1"></a><span class="fu">summary</span>(md1)<span class="sc">$</span>adj.r.squared</span></code></pre></div>
<pre><code>## [1] 0.9892266</code></pre>
<p>多项式回归</p>
<p><span class="math display">\[y=a+bx+cx^2\]</span></p>
<div class="sourceCode" id="cb352"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb352-1"><a href="回归分析.html#cb352-1" tabindex="-1"></a>md2<span class="ot">=</span><span class="fu">lm</span>(y<span class="sc">~</span>x<span class="sc">+</span><span class="fu">I</span>(x<span class="sc">^</span><span class="dv">2</span>))</span>
<span id="cb352-2"><a href="回归分析.html#cb352-2" tabindex="-1"></a><span class="fu">summary</span>(md2)</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = y ~ x + I(x^2))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.5985 -0.7169 -0.1012  0.7409  2.6941 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(&gt;|t|)    
## (Intercept) 1130.7632     0.7419 1524.18   &lt;2e-16 ***
## x            -20.6471     0.5414  -38.13   &lt;2e-16 ***
## I(x^2)         1.0944     0.0889   12.31   &lt;2e-16 ***
## ---
## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
## 
## Residual standard error: 1.081 on 97 degrees of freedom
## Multiple R-squared:  0.9958, Adjusted R-squared:  0.9958 
## F-statistic: 1.16e+04 on 2 and 97 DF,  p-value: &lt; 2.2e-16</code></pre>
<div class="sourceCode" id="cb354"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb354-1"><a href="回归分析.html#cb354-1" tabindex="-1"></a><span class="fu">plot</span>(<span class="at">x=</span>x,<span class="at">y=</span>y)</span>
<span id="cb354-2"><a href="回归分析.html#cb354-2" tabindex="-1"></a><span class="fu">abline</span>(md1)</span>
<span id="cb354-3"><a href="回归分析.html#cb354-3" tabindex="-1"></a>y_pred<span class="ot">=</span><span class="fu">predict</span>(md2)</span>
<span id="cb354-4"><a href="回归分析.html#cb354-4" tabindex="-1"></a><span class="fu">lines</span>(<span class="at">x=</span>x,<span class="at">y=</span>y_pred,<span class="at">col=</span><span class="dv">2</span>)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-133-1.png" width="672" /></p>
</div>
</div>
<div id="多元回归" class="section level2 hasAnchor" number="9.2">
<h2><span class="header-section-number">9.2</span> 多元回归<a href="回归分析.html#多元回归" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p><span class="math display">\[y=a+bx+cz+\epsilon\]</span></p>
<div class="sourceCode" id="cb355"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb355-1"><a href="回归分析.html#cb355-1" tabindex="-1"></a>da<span class="ot">=</span><span class="fu">read.table</span>(<span class="st">&quot;./_data/ozone.data.txt&quot;</span>,<span class="at">header=</span>T)</span>
<span id="cb355-2"><a href="回归分析.html#cb355-2" tabindex="-1"></a><span class="fu">str</span>(da)</span></code></pre></div>
<pre><code>## &#39;data.frame&#39;:    111 obs. of  4 variables:
##  $ rad  : int  190 118 149 313 299 99 19 256 290 274 ...
##  $ temp : int  67 72 74 62 65 59 61 69 66 68 ...
##  $ wind : num  7.4 8 12.6 11.5 8.6 13.8 20.1 9.7 9.2 10.9 ...
##  $ ozone: int  41 36 12 18 23 19 8 16 11 14 ...</code></pre>
<div class="sourceCode" id="cb357"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb357-1"><a href="回归分析.html#cb357-1" tabindex="-1"></a><span class="fu">pairs</span>(da,<span class="at">panel=</span>panel.smooth)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-135-1.png" width="672" /></p>
<div class="sourceCode" id="cb358"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb358-1"><a href="回归分析.html#cb358-1" tabindex="-1"></a><span class="co">#从最复杂的模型开始</span></span>
<span id="cb358-2"><a href="回归分析.html#cb358-2" tabindex="-1"></a>md1<span class="ot">=</span><span class="fu">lm</span>(ozone<span class="sc">~</span>rad<span class="sc">+</span>temp<span class="sc">+</span>wind<span class="sc">+</span><span class="fu">I</span>(rad<span class="sc">^</span><span class="dv">2</span>)<span class="sc">+</span><span class="fu">I</span>(temp<span class="sc">^</span><span class="dv">2</span>)<span class="sc">+</span><span class="fu">I</span>(wind<span class="sc">^</span><span class="dv">2</span>)<span class="sc">+</span>rad<span class="sc">:</span>temp<span class="sc">+</span>rad<span class="sc">:</span>wind<span class="sc">+</span>temp<span class="sc">:</span>wind<span class="sc">+</span>rad<span class="sc">:</span>temp<span class="sc">:</span>wind,<span class="at">data=</span>da)</span>
<span id="cb358-3"><a href="回归分析.html#cb358-3" tabindex="-1"></a>md1</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = ozone ~ rad + temp + wind + I(rad^2) + I(temp^2) + 
##     I(wind^2) + rad:temp + rad:wind + temp:wind + rad:temp:wind, 
##     data = da)
## 
## Coefficients:
##   (Intercept)            rad           temp           wind       I(rad^2)  
##     5.683e+02     -3.117e-01     -1.076e+01     -3.237e+01     -3.619e-04  
##     I(temp^2)      I(wind^2)       rad:temp       rad:wind      temp:wind  
##     5.833e-02      6.106e-01      8.403e-03      2.054e-02      2.377e-01  
## rad:temp:wind  
##    -4.324e-04</code></pre>
<div class="sourceCode" id="cb360"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb360-1"><a href="回归分析.html#cb360-1" tabindex="-1"></a><span class="fu">summary</span>(md1)</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = ozone ~ rad + temp + wind + I(rad^2) + I(temp^2) + 
##     I(wind^2) + rad:temp + rad:wind + temp:wind + rad:temp:wind, 
##     data = da)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -38.894 -11.205  -2.736   8.809  70.551 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(&gt;|t|)    
## (Intercept)    5.683e+02  2.073e+02   2.741  0.00725 ** 
## rad           -3.117e-01  5.585e-01  -0.558  0.57799    
## temp          -1.076e+01  4.303e+00  -2.501  0.01401 *  
## wind          -3.237e+01  1.173e+01  -2.760  0.00687 ** 
## I(rad^2)      -3.619e-04  2.573e-04  -1.407  0.16265    
## I(temp^2)      5.833e-02  2.396e-02   2.435  0.01668 *  
## I(wind^2)      6.106e-01  1.469e-01   4.157 6.81e-05 ***
## rad:temp       8.403e-03  7.512e-03   1.119  0.26602    
## rad:wind       2.054e-02  4.892e-02   0.420  0.67552    
## temp:wind      2.377e-01  1.367e-01   1.739  0.08519 .  
## rad:temp:wind -4.324e-04  6.595e-04  -0.656  0.51358    
## ---
## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
## 
## Residual standard error: 17.82 on 100 degrees of freedom
## Multiple R-squared:  0.7394, Adjusted R-squared:  0.7133 
## F-statistic: 28.37 on 10 and 100 DF,  p-value: &lt; 2.2e-16</code></pre>
<div id="变量筛选" class="section level3 hasAnchor" number="9.2.1">
<h3><span class="header-section-number">9.2.1</span> 变量筛选<a href="回归分析.html#变量筛选" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>原则：将那些不显著且会让模型复杂的因子去除，去除顺序依据这些因子的复杂程度。</p>
<p>例如，三者的交互作用（rad:wind:temp）极其复杂，因此如果这一项不显著，那么应该最先被去除。</p>
<div class="sourceCode" id="cb362"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb362-1"><a href="回归分析.html#cb362-1" tabindex="-1"></a>md2<span class="ot">=</span><span class="fu">update</span>(md1, .<span class="sc">~</span>.<span class="sc">-</span>rad<span class="sc">:</span>temp<span class="sc">:</span>wind)</span>
<span id="cb362-2"><a href="回归分析.html#cb362-2" tabindex="-1"></a><span class="fu">summary</span>(md2)</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = ozone ~ rad + temp + wind + I(rad^2) + I(temp^2) + 
##     I(wind^2) + rad:temp + rad:wind + temp:wind, data = da)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -39.611 -11.455  -2.901   8.548  70.325 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(&gt;|t|)    
## (Intercept)  5.245e+02  1.957e+02   2.680   0.0086 ** 
## rad          2.628e-02  2.142e-01   0.123   0.9026    
## temp        -1.021e+01  4.209e+00  -2.427   0.0170 *  
## wind        -2.802e+01  9.645e+00  -2.906   0.0045 ** 
## I(rad^2)    -3.388e-04  2.541e-04  -1.333   0.1855    
## I(temp^2)    5.953e-02  2.382e-02   2.499   0.0141 *  
## I(wind^2)    6.173e-01  1.461e-01   4.225 5.25e-05 ***
## rad:temp     3.750e-03  2.459e-03   1.525   0.1303    
## rad:wind    -1.127e-02  6.277e-03  -1.795   0.0756 .  
## temp:wind    1.734e-01  9.497e-02   1.825   0.0709 .  
## ---
## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
## 
## Residual standard error: 17.77 on 101 degrees of freedom
## Multiple R-squared:  0.7383, Adjusted R-squared:  0.715 
## F-statistic: 31.66 on 9 and 101 DF,  p-value: &lt; 2.2e-16</code></pre>
</div>
<div id="因子之间的相对重要性" class="section level3 hasAnchor" number="9.2.2">
<h3><span class="header-section-number">9.2.2</span> 因子之间的相对重要性<a href="回归分析.html#因子之间的相对重要性" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>多元回归的回归系数一定程度上代表了对应的解释变量对应变量的影响程度，但前提是这些解释变量都处于可比的程度。</p>
<p>这里的可比是指他们都具有相同的变化程度，即具有相同的标准差和均值。这可以通过scale函数来实现。</p>
<div class="sourceCode" id="cb364"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb364-1"><a href="回归分析.html#cb364-1" tabindex="-1"></a>da[,<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>]<span class="ot">=</span><span class="fu">scale</span>(da[,<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>])</span>
<span id="cb364-2"><a href="回归分析.html#cb364-2" tabindex="-1"></a><span class="fu">round</span>(<span class="fu">apply</span>(da[,<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>],<span class="dv">2</span>,mean),<span class="dv">2</span>)</span></code></pre></div>
<pre><code>##  rad temp wind 
##    0    0    0</code></pre>
<div class="sourceCode" id="cb366"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb366-1"><a href="回归分析.html#cb366-1" tabindex="-1"></a><span class="fu">apply</span>(da[,<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>],<span class="dv">2</span>,sd)</span></code></pre></div>
<pre><code>##  rad temp wind 
##    1    1    1</code></pre>
<div class="sourceCode" id="cb368"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb368-1"><a href="回归分析.html#cb368-1" tabindex="-1"></a>md1<span class="ot">=</span><span class="fu">lm</span>(ozone<span class="sc">~</span>rad<span class="sc">*</span>temp<span class="sc">*</span>wind<span class="sc">+</span><span class="fu">I</span>(rad<span class="sc">^</span><span class="dv">2</span>)<span class="sc">+</span><span class="fu">I</span>(temp<span class="sc">^</span><span class="dv">2</span>)<span class="sc">+</span><span class="fu">I</span>(wind<span class="sc">^</span><span class="dv">2</span>),<span class="at">data=</span>da)</span>
<span id="cb368-2"><a href="回归分析.html#cb368-2" tabindex="-1"></a><span class="fu">summary</span>(md1)</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = ozone ~ rad * temp * wind + I(rad^2) + I(temp^2) + 
##     I(wind^2), data = da)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -38.894 -11.205  -2.736   8.809  70.551 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(&gt;|t|)    
## (Intercept)     33.319      3.245  10.269  &lt; 2e-16 ***
## rad              7.107      2.162   3.288  0.00139 ** 
## temp            13.684      2.285   5.989 3.33e-08 ***
## wind           -14.839      2.415  -6.145 1.64e-08 ***
## I(rad^2)        -3.007      2.138  -1.407  0.16265    
## I(temp^2)        5.298      2.176   2.435  0.01668 *  
## I(wind^2)        7.736      1.861   4.157 6.81e-05 ***
## rad:temp         3.566      2.193   1.626  0.10708    
## rad:wind        -4.250      2.234  -1.902  0.06004 .  
## temp:wind        5.351      3.330   1.607  0.11124    
## rad:temp:wind   -1.337      2.039  -0.656  0.51358    
## ---
## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
## 
## Residual standard error: 17.82 on 100 degrees of freedom
## Multiple R-squared:  0.7394, Adjusted R-squared:  0.7133 
## F-statistic: 28.37 on 10 and 100 DF,  p-value: &lt; 2.2e-16</code></pre>
<div class="sourceCode" id="cb370"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb370-1"><a href="回归分析.html#cb370-1" tabindex="-1"></a>md2<span class="ot">=</span><span class="fu">step</span>(md1)</span></code></pre></div>
<pre><code>## Start:  AIC=649.8
## ozone ~ rad * temp * wind + I(rad^2) + I(temp^2) + I(wind^2)
## 
##                 Df Sum of Sq   RSS    AIC
## - rad:temp:wind  1     136.4 31879 648.28
## &lt;none&gt;                       31742 649.80
## - I(rad^2)       1     628.0 32370 649.98
## - I(temp^2)      1    1881.5 33624 654.19
## - I(wind^2)      1    5486.1 37228 665.50
## 
## Step:  AIC=648.28
## ozone ~ rad + temp + wind + I(rad^2) + I(temp^2) + I(wind^2) + 
##     rad:temp + rad:wind + temp:wind
## 
##             Df Sum of Sq   RSS    AIC
## - I(rad^2)   1     560.9 32440 648.21
## &lt;none&gt;                   31879 648.28
## - rad:temp   1     734.2 32613 648.81
## - rad:wind   1    1017.1 32896 649.76
## - temp:wind  1    1051.8 32930 649.88
## - I(temp^2)  1    1971.0 33850 652.94
## - I(wind^2)  1    5634.0 37513 664.34
## 
## Step:  AIC=648.21
## ozone ~ rad + temp + wind + I(temp^2) + I(wind^2) + rad:temp + 
##     rad:wind + temp:wind
## 
##             Df Sum of Sq   RSS    AIC
## - rad:temp   1     538.8 32978 648.04
## &lt;none&gt;                   32440 648.21
## - temp:wind  1     883.2 33323 649.20
## - rad:wind   1    1033.8 33473 649.70
## - I(temp^2)  1    1816.7 34256 652.26
## - I(wind^2)  1    5360.9 37800 663.19
## 
## Step:  AIC=648.04
## ozone ~ rad + temp + wind + I(temp^2) + I(wind^2) + rad:wind + 
##     temp:wind
## 
##             Df Sum of Sq   RSS    AIC
## &lt;none&gt;                   32978 648.04
## - temp:wind  1    1033.1 34011 649.47
## - rad:wind   1    1587.8 34566 651.26
## - I(temp^2)  1    2931.6 35910 655.50
## - I(wind^2)  1    5780.7 38759 663.97</code></pre>
<div class="sourceCode" id="cb372"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb372-1"><a href="回归分析.html#cb372-1" tabindex="-1"></a>md3<span class="ot">=</span><span class="fu">update</span>(md2, .<span class="sc">~</span>.<span class="sc">-</span>temp<span class="sc">:</span>wind)</span>
<span id="cb372-2"><a href="回归分析.html#cb372-2" tabindex="-1"></a><span class="fu">summary</span>(md3)</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = ozone ~ rad + temp + wind + I(temp^2) + I(wind^2) + 
##     rad:wind, data = da)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -44.478 -10.735  -2.437   9.685  77.543 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(&gt;|t|)    
## (Intercept)   32.370      2.482  13.041  &lt; 2e-16 ***
## rad            6.913      1.889   3.660 0.000399 ***
## temp          15.178      2.151   7.057 1.96e-10 ***
## wind         -14.529      2.156  -6.741 9.04e-10 ***
## I(temp^2)      3.963      1.645   2.410 0.017731 *  
## I(wind^2)      5.448      1.289   4.226 5.12e-05 ***
## rad:wind      -3.186      1.876  -1.698 0.092507 .  
## ---
## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
## 
## Residual standard error: 18.08 on 104 degrees of freedom
## Multiple R-squared:  0.7208, Adjusted R-squared:  0.7047 
## F-statistic: 44.74 on 6 and 104 DF,  p-value: &lt; 2.2e-16</code></pre>
<div class="sourceCode" id="cb374"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb374-1"><a href="回归分析.html#cb374-1" tabindex="-1"></a>md4<span class="ot">=</span><span class="fu">update</span>(md3, .<span class="sc">~</span>.<span class="sc">-</span>rad<span class="sc">:</span>wind)</span>
<span id="cb374-2"><a href="回归分析.html#cb374-2" tabindex="-1"></a><span class="fu">summary</span>(md4)</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = ozone ~ rad + temp + wind + I(temp^2) + I(wind^2), 
##     data = da)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -48.044 -10.796  -4.138   8.131  80.098 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(&gt;|t|)    
## (Intercept)   31.674      2.470  12.824  &lt; 2e-16 ***
## rad            6.003      1.827   3.285  0.00139 ** 
## temp          15.233      2.170   7.021 2.26e-10 ***
## wind         -14.810      2.168  -6.830 5.71e-10 ***
## I(temp^2)      4.634      1.611   2.876  0.00488 ** 
## I(wind^2)      5.886      1.274   4.619 1.10e-05 ***
## ---
## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
## 
## Residual standard error: 18.25 on 105 degrees of freedom
## Multiple R-squared:  0.713,  Adjusted R-squared:  0.6994 
## F-statistic: 52.18 on 5 and 105 DF,  p-value: &lt; 2.2e-16</code></pre>
</div>
<div id="模型诊断" class="section level3 hasAnchor" number="9.2.3">
<h3><span class="header-section-number">9.2.3</span> 模型诊断<a href="回归分析.html#模型诊断" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb376"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb376-1"><a href="回归分析.html#cb376-1" tabindex="-1"></a><span class="fu">plot</span>(md4)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-144-1.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-144-2.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-144-3.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-144-4.png" width="672" /></p>
<p>从第一个图可知，目前残差的方差是非齐性的。
解决这一问题途径之一log</p>
<div class="sourceCode" id="cb377"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb377-1"><a href="回归分析.html#cb377-1" tabindex="-1"></a>da<span class="sc">$</span>lozone<span class="ot">=</span><span class="fu">log</span>(da<span class="sc">$</span>ozone)</span>
<span id="cb377-2"><a href="回归分析.html#cb377-2" tabindex="-1"></a>md1<span class="ot">=</span><span class="fu">step</span>(<span class="fu">lm</span>(lozone<span class="sc">~</span>rad<span class="sc">*</span>temp<span class="sc">*</span>wind<span class="sc">+</span><span class="fu">I</span>(rad<span class="sc">^</span><span class="dv">2</span>)<span class="sc">+</span><span class="fu">I</span>(temp<span class="sc">^</span><span class="dv">2</span>)<span class="sc">+</span><span class="fu">I</span>(wind<span class="sc">^</span><span class="dv">2</span>),<span class="at">data=</span>da), <span class="at">trace=</span><span class="dv">0</span>)</span>
<span id="cb377-3"><a href="回归分析.html#cb377-3" tabindex="-1"></a><span class="fu">summary</span>(md1)</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = lozone ~ rad + temp + wind + I(rad^2) + I(wind^2), 
##     data = da)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.85551 -0.25578  0.00248  0.31349  1.16251 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(&gt;|t|)    
## (Intercept)  3.44421    0.07836  43.951  &lt; 2e-16 ***
## rad          0.18520    0.05268   3.516 0.000649 ***
## temp         0.39963    0.05944   6.723 9.52e-10 ***
## wind        -0.26425    0.05688  -4.646 9.86e-06 ***
## I(rad^2)    -0.12216    0.05595  -2.183 0.031246 *  
## I(wind^2)    0.09362    0.03274   2.859 0.005126 ** 
## ---
## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
## 
## Residual standard error: 0.4851 on 105 degrees of freedom
## Multiple R-squared:  0.7004, Adjusted R-squared:  0.6861 
## F-statistic:  49.1 on 5 and 105 DF,  p-value: &lt; 2.2e-16</code></pre>
<div class="sourceCode" id="cb379"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb379-1"><a href="回归分析.html#cb379-1" tabindex="-1"></a><span class="fu">plot</span>(md1)</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-146-1.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-146-2.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-146-3.png" width="672" /><img src="_main_files/figure-html/unnamed-chunk-146-4.png" width="672" /></p>
</div>
<div id="包含类型变量的多元回归" class="section level3 hasAnchor" number="9.2.4">
<h3><span class="header-section-number">9.2.4</span> 包含类型变量的多元回归<a href="回归分析.html#包含类型变量的多元回归" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<pre><code>## &#39;data.frame&#39;:    111 obs. of  6 variables:
##  $ rad   : num  0.057 -0.733 -0.393 1.406 1.253 ...
##  $ temp  : num  -1.133 -0.608 -0.398 -1.657 -1.342 ...
##  $ wind  : num  -0.713 -0.545 0.748 0.439 -0.376 ...
##  $ ozone : int  41 36 12 18 23 19 8 16 11 14 ...
##  $ lozone: num  3.71 3.58 2.48 2.89 3.14 ...
##  $ iscold: logi  TRUE TRUE TRUE TRUE TRUE TRUE ...</code></pre>
<div class="sourceCode" id="cb381"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb381-1"><a href="回归分析.html#cb381-1" tabindex="-1"></a>md1<span class="ot">=</span><span class="fu">lm</span>(lozone<span class="sc">~</span>rad<span class="sc">*</span>iscold,<span class="at">data=</span>da)</span>
<span id="cb381-2"><a href="回归分析.html#cb381-2" tabindex="-1"></a><span class="fu">summary</span>(md1)</span></code></pre></div>
<pre><code>## 
## Call:
## lm(formula = lozone ~ rad * iscold, data = da)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.39844 -0.31293  0.03162  0.43074  1.04111 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(&gt;|t|)    
## (Intercept)     3.86341    0.07765  49.755  &lt; 2e-16 ***
## rad             0.37601    0.09831   3.825 0.000221 ***
## iscoldTRUE     -1.03214    0.11487  -8.985 1.01e-14 ***
## rad:iscoldTRUE -0.15286    0.12102  -1.263 0.209311    
## ---
## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
## 
## Residual standard error: 0.5851 on 107 degrees of freedom
## Multiple R-squared:  0.5558, Adjusted R-squared:  0.5434 
## F-statistic: 44.63 on 3 and 107 DF,  p-value: &lt; 2.2e-16</code></pre>
<div class="sourceCode" id="cb383"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb383-1"><a href="回归分析.html#cb383-1" tabindex="-1"></a><span class="co">#TODO 画上两条回归直线</span></span></code></pre></div>
</div>
</div>
<div id="广义线性回归" class="section level2 hasAnchor" number="9.3">
<h2><span class="header-section-number">9.3</span> 广义线性回归<a href="回归分析.html#广义线性回归" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="目前的困境" class="section level3 hasAnchor" number="9.3.1">
<h3><span class="header-section-number">9.3.1</span> 目前的困境<a href="回归分析.html#目前的困境" class="anchor-section" aria-label="Anchor link to header"></a></h3>
</div>
<div id="理论扩展" class="section level3 hasAnchor" number="9.3.2">
<h3><span class="header-section-number">9.3.2</span> 理论扩展<a href="回归分析.html#理论扩展" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>一般的线性回归
<span class="math display">\[y=a+bx+\epsilon\]</span>
且假设<span class="math inline">\(\epsilon\)</span>是一个标准正态分布。</p>
<p>令<span class="math inline">\(\eta=a+bx\)</span>，则<span class="math inline">\(y\)</span>就可以表示为<span class="math inline">\(N(\eta,\delta)\)</span>。</p>
<p>从上述式子可以看出，回归其实只是在寻找一个y的均值的一个函数，这里的这个均值就是<span class="math inline">\(\eta\)</span>。</p>
<p>那么，我们就可以假设<span class="math inline">\(y\)</span>不一定要符合正态分布了。我们只要建立<span class="math inline">\(y\)</span>的均值与<span class="math inline">\(x\)</span>之间的关系，即要寻找这样<span class="math inline">\(\eta=g(x)\)</span>一个函数。我们常称这个函数为链接函数(link function)。</p>
<p>而<span class="math inline">\(y\)</span>是一个符合某种分布的随机变量。目前，R能处理的分布是指数分布族。</p>
</div>
<div id="binary数据例子" class="section level3 hasAnchor" number="9.3.3">
<h3><span class="header-section-number">9.3.3</span> binary数据例子<a href="回归分析.html#binary数据例子" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb384"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb384-1"><a href="回归分析.html#cb384-1" tabindex="-1"></a>island<span class="ot">=</span><span class="fu">read.table</span>(<span class="st">&quot;./_data/island.txt&quot;</span>,<span class="at">header=</span>T)</span>
<span id="cb384-2"><a href="回归分析.html#cb384-2" tabindex="-1"></a><span class="fu">str</span>(island)</span></code></pre></div>
<pre><code>## &#39;data.frame&#39;:    50 obs. of  6 variables:
##  $ incidence  : int  1 0 1 0 0 1 1 0 1 1 ...
##  $ area       : num  7.93 1.92 2.04 4.78 1.54 ...
##  $ isolation  : num  3.32 7.55 5.88 5.93 5.31 ...
##  $ quality    : num  3 8 8 2 3 4 6 7 3 3 ...
##  $ enemy      : num  18.15 9.27 9.21 14.25 8.68 ...
##  $ competitors: num  8.43 252.31 304.36 3.13 20.92 ...</code></pre>
<div class="sourceCode" id="cb386"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb386-1"><a href="回归分析.html#cb386-1" tabindex="-1"></a>md1<span class="ot">=</span><span class="fu">glm</span>(incidence<span class="sc">~</span>area,<span class="at">family =</span> <span class="fu">binomial</span>(<span class="at">link =</span> <span class="st">&quot;logit&quot;</span>),<span class="at">data=</span>island)</span>
<span id="cb386-2"><a href="回归分析.html#cb386-2" tabindex="-1"></a>md1</span></code></pre></div>
<pre><code>## 
## Call:  glm(formula = incidence ~ area, family = binomial(link = &quot;logit&quot;), 
##     data = island)
## 
## Coefficients:
## (Intercept)         area  
##     -2.1554       0.6272  
## 
## Degrees of Freedom: 49 Total (i.e. Null);  48 Residual
## Null Deviance:       68.03 
## Residual Deviance: 50.17     AIC: 54.17</code></pre>
<div class="sourceCode" id="cb388"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb388-1"><a href="回归分析.html#cb388-1" tabindex="-1"></a><span class="fu">summary</span>(md1)</span></code></pre></div>
<pre><code>## 
## Call:
## glm(formula = incidence ~ area, family = binomial(link = &quot;logit&quot;), 
##     data = island)
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&gt;|z|)    
## (Intercept)  -2.1554     0.7545  -2.857 0.004278 ** 
## area          0.6272     0.1861   3.370 0.000753 ***
## ---
## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 68.029  on 49  degrees of freedom
## Residual deviance: 50.172  on 48  degrees of freedom
## AIC: 54.172
## 
## Number of Fisher Scoring iterations: 5</code></pre>
</div>
<div id="展示回归结果" class="section level3 hasAnchor" number="9.3.4">
<h3><span class="header-section-number">9.3.4</span> 展示回归结果<a href="回归分析.html#展示回归结果" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb390"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb390-1"><a href="回归分析.html#cb390-1" tabindex="-1"></a><span class="fu">plot</span>(<span class="at">x=</span>island<span class="sc">$</span>area, <span class="at">y=</span>island<span class="sc">$</span>incidence)</span>
<span id="cb390-2"><a href="回归分析.html#cb390-2" tabindex="-1"></a>re<span class="ot">=</span><span class="fu">coef</span>(md1)</span>
<span id="cb390-3"><a href="回归分析.html#cb390-3" tabindex="-1"></a>eta<span class="ot">=</span>re[<span class="dv">1</span>]<span class="sc">+</span>island<span class="sc">$</span>area<span class="sc">*</span>re[<span class="dv">2</span>]</span>
<span id="cb390-4"><a href="回归分析.html#cb390-4" tabindex="-1"></a>pi<span class="ot">=</span><span class="dv">1</span><span class="sc">/</span>(<span class="fu">exp</span>(<span class="sc">-</span><span class="dv">1</span><span class="sc">*</span>eta)<span class="sc">+</span><span class="dv">1</span>) <span class="co">#这是link function的反函数</span></span>
<span id="cb390-5"><a href="回归分析.html#cb390-5" tabindex="-1"></a>arei<span class="ot">=</span><span class="fu">order</span>(island<span class="sc">$</span>area)</span>
<span id="cb390-6"><a href="回归分析.html#cb390-6" tabindex="-1"></a><span class="fu">lines</span>(<span class="at">x=</span>island<span class="sc">$</span>area[arei],<span class="at">y=</span>pi[arei])</span></code></pre></div>
<p><img src="_main_files/figure-html/unnamed-chunk-152-1.png" width="672" /></p>
</div>
</div>
<div id="混合效应模型" class="section level2 hasAnchor" number="9.4">
<h2><span class="header-section-number">9.4</span> 混合效应模型<a href="回归分析.html#混合效应模型" class="anchor-section" aria-label="Anchor link to header"></a></h2>
</div>
<div id="结构方程" class="section level2 hasAnchor" number="9.5">
<h2><span class="header-section-number">9.5</span> 结构方程<a href="回归分析.html#结构方程" class="anchor-section" aria-label="Anchor link to header"></a></h2>
</div>
<div id="作业-2" class="section level2 hasAnchor" number="9.6">
<h2><span class="header-section-number">9.6</span> 作业<a href="回归分析.html#作业-2" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<ol style="list-style-type: decimal">
<li><p>’treegrowth.txt’文件中包含了一个样地内所有树木，两次的胸径测量值。请你建立一个模型，用第一次的胸径来预测解释第二次的胸径大小。</p></li>
<li><p>现有一套数据，其中x为1到10的整数，y为来自[0,1]区间的符合均匀分布的随机变量。请用多项式回归，建立一个x是自变量，y是相应变量的最佳模型。</p></li>
</ol>

</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="经典统计检验.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="谱系分析.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/USERNAME/REPO/edit/BRANCH/08-regression.Rmd",
"text": "编辑"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["_main.epub"],
"search": {
"engine": "fuse",
"options": null
},
"toc": {
"collapse": "subsection"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/latest.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
